<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/6/27
 * Time: 18:42
 */
namespace app\admin\controller;
use app\common\model\Banner as BannerModel;
use think\Request;
class Advertisement extends \app\admin\controller\Base {

    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list,My97DatePicker'),
        'add'   => array('css' => '2,3,4,5,9', 'js' => 'ueditor_config,ueditor_all,ueditor_lang'),
        'update'   => array('css' => '2,3,4,5,9', 'js' => 'ueditor_config,ueditor_all,ueditor_lang'),
        'detail' => array('css' => '2,3,4,5,9', 'js' => 'ueditor_config,ueditor_all,ueditor_lang'),
    );
    
    //状态
    public static $status = [
        '1' => '启用',
        '2' => '禁用'
    ];

    /***
     *首页列表
     */
    public function index(){
        $BannerModel = new BannerModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        if(!$page) {
            $pages = 0;//进入后台没有pages时，page置0
        } else {
            $pages = ($page - 1) * 15;//用于生成序号
        }
        $where = $this->getWhere($search);
        $list = $BannerModel->relData($where, 15);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $wherePro['level'] = 1;
        $this->assign(
            array(
                'action'=>$action,
                'list'=>$list,
                'page'=>$page,
                'search' => $search,
                'pages' => $pages,
                'status' => self::$status
            )
        );
        return $this->fetch();
    }
    
    public function detail(){
        $banner_id = input('param.banner_id');
        $page = input('param.page');
        $bannerModel = new BannerModel();
        $list = $bannerModel->getOne($banner_id);
        $this->assign(array(
            'list' => $list,
            'page' => $page,
        ));
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     */
   public function add(){
       $BannerModel = new BannerModel();
       if(Request::instance()->isPost()){
           if($_FILES['file']['name'][0]=='') {
               return $this->api_dbError("上传文件不能为空");
           }
           $info = $this->uploadFile($_FILES);
           $data['banner_img'] =$info;
           $data['banner_title'] = input('post.banner_title');
           $data['banner_sort'] = input('post.banner_sort');
           $data['banner_content'] = input('post.content');
           $data['banner_time'] = date('Y-m-d H:i:s',time());
           $data['banner_link'] = input('post.banner_link');
           $data['banner_titles'] = input('post.banner_titles');
           $res = $BannerModel->save($data);
           if ($res) {
               return $this->api_success("操作成功");
           } else {
               return $this->api_dbError("操作失败");
           }
       }
       return $this->fetch();
   }

   public function update(){
       $BannerModel = new BannerModel();
       if(Request::instance()->isPost()){
           if($_FILES['file']['name'][0]!='') {
               $info = $this->uploadFile($_FILES);
//               if($info['status']!=200) {
//                   return $this->api_dbError("上传文件格式不正确");
//               }
               $data['banner_img'] =$info;

           }
           $data['banner_id'] = input('post.id');
           $data['banner_title'] = input('post.banner_title');
           $data['banner_sort'] = input('post.banner_sort');
           $data['banner_content'] = input('post.content');
           $data['banner_time'] = date('Y-m-d H:i:s',time());
           $data['banner_link'] = input('post.banner_link');
           $data['banner_titles'] = input('post.banner_titles');
           $res = $BannerModel->update($data);
           if ($res) {
               return $this->api_success("操作成功");
           } else {
               return $this->api_dbError("操作失败");
           }
       }
       $id = input('get.id');
       $list = $BannerModel->getOne($id);
       $this->assign('id', $id);
       $this->assign('ad', $list);
       return $this->fetch();
   }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function delBanner(){
        $ids = strtoarray(input('post.ids'));
        $BannerModel = new BannerModel();
        $where['banner_id'] = array('In', $ids);
        $result = $BannerModel->where($where)->delete();
        if ($result) {
            return $this->api_success('操作成功');
        } else {
            return $this->api_dbError();
        }
    }

    /**
     * @return \think\response\Json
     * 排序
     */
    public function sortBanner(){
        $sort = input('post.sort');
        $wine_id = input('post.wine_id');
        $BannerModel = new BannerModel();
        $where['banner_id'] = $wine_id;
        $res = $BannerModel->where($where)->setField("banner_sort",$sort);
        if ($res) {
            return $this->api_success("操作排序成功");
        } else {
            return $this->api_dbError("操作排序失败");
        }
    }
    /***
     * @return \think\response\Json
     * 修改状态
     */
    public function statusBanner(){
        $status = input('post.status');
        $act = input('post.act');
        $BannerModel = new BannerModel();
        $where['banner_id'] = $act;
        $res = $BannerModel->where($where)->setField("banner_status",$status);
        if ($res) {
            return $this->api_success("操作成功");
        } else {
            return $this->api_dbError("操作失败");
        }
    }
    
    /**
     * 获取筛选数组
     * @param $search  查询条件
     * @return array
     */
    public function getWhere($search)
    {
        $whereS = [];
        if($search[0] != '') {
            $whereS['banner_id|banner_titles'] = ['like', '%' . $search[0] . '%'];
        }
        if($search[1] != '' && $search[2] != '') {
            $whereS['banner_time'] = array('between', array($search[1], $search[2]));
        } elseif($search[1] != '') {
            $whereS['banner_time'] = ['>', $search[1]];
            
        } elseif($search[2] != '') {
            $whereS['banner_time'] = ['<', $search[2]];
        }
        if($search[3] != ''){
            $whereS['banner_status'] = ['=', $search[3]];
        }
        return $whereS;
    }
}